package com.cuitrip.business.home.travel.model;

import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.cuitrip.business.home.trip.model.Facet;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by sheena on 15/11/6.
 */
public class SelectInfo implements Serializable {

    public static final String DEFAULT_GENDER = "-1";
    public static final String MALE = "1";
    public static final String FEMAIL = "2";


    public JSONObject search;

    public List<Facet> facetList = new ArrayList<>();

    public List<FilterInfo> filterInfoList = new ArrayList<>();


    public List<FilterInfo> getFilterInfoList() {
        return filterInfoList;
    }

    public void setFilterInfoList(List<FilterInfo> filterInfoList) {
        this.filterInfoList = filterInfoList;
    }

    public SelectInfo() {
        search = new JSONObject();
    }

    public void putSearchExtra(String key, Object value) {
        search.put(key, value);
    }

    public String getSearchStringExtra(String key) {
        return search.getString(key);
    }

    public boolean isEmpty() {
        int existParam = 0;
        for (FilterInfo filterInfo : filterInfoList) {
            if (!filterInfo.getSelectedValueList().isEmpty()) {
                existParam++;
            }
        }
        return existParam == 0
                && search.isEmpty();
    }

    public String toFilterJSON() {
        JSONObject jsonObject = new JSONObject();
        for (FilterInfo filterInfo : filterInfoList) {
            if (FilterInfo.containsType(filterInfo.getSearchKey())){
                FilterInfo.FilterType filterType = FilterInfo.getFilterType(filterInfo.getSearchKey());
                String value  = filterType.getOutPutValue(filterInfo);
                if (!TextUtils.isEmpty(value)){
                    jsonObject.put(filterType.key, value);
                }

            }
        }


        for (Map.Entry<String, Object> entry : search.entrySet()) {
            if ("cityAbbr".equals(entry.getKey()) || "keyWord".equals(entry.getKey())) {
                continue;
            }
            jsonObject.put(entry.getKey(), entry.getValue());

        }
        return jsonObject.toJSONString();
    }

    public boolean hasSearchStringExtra(String key) {
        return search.containsKey(key);
    }

    public Facet retrieveFacetFrom(String key){
        for (Facet facet : facetList){
            if (facet.getKey().equals(key)){
                return facet;
            }
        }
        return null;
    }
}
